Skip to content

Worker-safety check: see a scriptblock through parentheses, casts and GetNewClosure - #97

Merged
HalRiveria-Bot merged 7 commits into
mainfrom
fix/delegate-cast-wrapped
Dec 1, 2026
Merged

HalRiveria-Bot merged 7 commits into
mainfrom
fix/delegate-cast-wrapped

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

The worker-safety check's DelegateCast rule flags a scriptblock passed to a .NET method or constructor, which .NET turns into a delegate that can hang or crash a worker. It missed one with anything around it: ({ 1 }), { 1 }.GetNewClosure(), ({ 1 }).GetNewClosure(), [scriptblock]{ 1 } or $sb.GetNewClosure() in the call, and $sb = ({ 1 }), $sb = ({ 1 }).GetNewClosure() or $sb = [scriptblock]{ 1 } passed later. { 1 }.GetNewClosure() in the call is the likeliest one in real code.

  • One private helper, Get-PkUnwrappedExpression, takes off parentheses, [scriptblock] casts and .GetNewClosure() calls, in any order. It's used on each argument and on an assignment's right side, which had its own partial version of this.
  • The README says what the rule sees through.

Invoke-PkRunspaceJobs refuses a worker this newly flags. # pk:ignore on the line still accepts one whose method really takes a scriptblock.

Tests

Section P tries eight wrapped forms, each passed directly and through a variable, and four ways to wrap the variable itself. It also checks that other parenthesized or cast values, .ForEach(($sb)), .Add(($sb)) and a # pk:ignore line aren't flagged.

  • On the old code, 5.1 and 7 both fail: a wrapped scriptblock passed to .NET is flagged, written in the call or through a variable (missed: passed ({ 1 }) | ...). { 1 }.GetNewClosure() assigned to a variable was the only form seen.
  • With the fix, nothing is missed.
  • Full harness: 711 passed on Windows PowerShell 5.1 and 711 passed on PowerShell 7.6.2, no skips. Testing/readme-examples/run.ps1 passes on both, so no README worker is newly flagged.

🤖 Generated with Claude Code

HalRiveria and others added 7 commits December 1, 2026 16:12
Two of the batch's own closures called Complete-PkProcess and Write-PkMessage
by name. A .GetNewClosure() block only sees global functions, so a batch failed
with "'Complete-PkProcess' is not recognized" when the kit was loaded for one
module only. They're captured up front now, like the private helpers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A worker given the function (-SharedFunctionNames takes it) could set a handler
made in its own runspace, which the main thread then ran for every message.
Running another runspace's scriptblock can hang or crash PowerShell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It only looked for an abort after its first 200 ms wait, so a program that
finished sooner ran to the end and returned normally, and a slower one ran for
about 200 ms before it was killed. It now throws before starting the program,
unless -IgnoreAbort is given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… GetNewClosure

DelegateCast missed a scriptblock passed to .NET as ({ 1 }), { 1 }.GetNewClosure(),
[scriptblock]{ 1 } and the like, in the call or through a variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit 71a5cba into main Dec 1, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/delegate-cast-wrapped branch December 1, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants